<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
<style>
    .container{
        width: 100%;
        height: 100%;
        display: flex;
        background-color: antiquewhite;
    }
    .item{
        width: 100px;
        height: 100px;
        margin: 10px;
        border:1px solid #000;
    }
</style>
</head>

<body>
    <div class="container">
        <h2 class="page-header">点击切换颜色</h2>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
    </div>

    <script>
       
        //获取div元素对象;
        let items = document.getElementsByClassName('item')
         
        //遍历元素对象;
        for(var i = 0;i<items.length;i++){
            items[i].onclick = function(){
                //修改当前元素的背景颜色;
                this.style.background = 'pink';
                //items[i].style.background = 'pink';

            }
        }
    </script>
</body>
</html>